package com.weixing.mall.pay.channel.wx;

import com.alibaba.fastjson.JSON;
import com.weixing.mall.pay.AbstractPayFactory;
import com.weixing.mall.pay.INotify;
import com.weixing.mall.pay.IPayment;
import com.weixing.mall.pay.channel.wx.payment.*;
import com.weixing.mall.pay.properties.PayProperties;
import com.weixing.mall.pay.properties.WxPayProperties;

import java.util.List;
import java.util.Map;

/**
 * @ClassName WxPayFactory
 * @Description
 * @Author Small
 * @Date 2019/12/4 19:32
 * @Version 1.0
 **/
public class WxPayFactory extends AbstractPayFactory {

    @Override
    public WxTradeTypeEnum initTradeType(String tradeType) {
        WxTradeTypeEnum tradeTypeEnum = Enum.valueOf(WxTradeTypeEnum.class,tradeType);
        return tradeTypeEnum;
    }

    @Override
    public IPayment getPayment(String tradeType, PayProperties payProperties) {
        WxTradeTypeEnum tradeTypeEnum = initTradeType(tradeType);
        WxPayProperties wxPayProperties = payProperties.getWx();
        WxPaySet paySet = convertProp(wxPayProperties);
        return getPayment(tradeTypeEnum, paySet);
    }


    private WxPaySet convertProp(WxPayProperties properties){
        WxPaySet paySet = new WxPaySet();
        paySet.setAppid(properties.getAppid());
        paySet.setAppletId(properties.getAppletAppid());
        paySet.setKey(properties.getKey());
        paySet.setMchid(properties.getMchid());
        paySet.setNotifyUrl(properties.getNotifyUrl());
        return paySet;
    }





    @Override
    public IPayment getPayment(String tradeType, String payParamJsonStr) {
        WxTradeTypeEnum tradeTypeEnum = initTradeType(tradeType);
        WxPaySet wxPaySet = JSON.parseObject(payParamJsonStr, WxPaySet.class);
        return getPayment(tradeTypeEnum, wxPaySet);
    }

    private IPayment getPayment(WxTradeTypeEnum tradeTypeEnum, WxPaySet wxPaySet) {
        IPayment payment = null;
        switch (tradeTypeEnum) {
            case H5:
                payment = new WxPayH5(wxPaySet);
                break;
            case APP:
                payment = new WxPayApp(wxPaySet);
                break;
            case XCX:
                payment = new WxPayXCX(wxPaySet);
                break;
            case JSAPI:
                payment = new WxPayJSAPI(wxPaySet);
                break;
            case NATIVE:
                payment = new WxPayNative(wxPaySet);
                break;
        }
        return payment;
    }

    @Override
    public INotify getNotify(PayProperties payProperties) {
        return new WxNotify(payProperties.getWx());
    }

    @Override
    public INotify getNotify(List<Map<String, Object>> setVoMapList) {
        return new WxNotify(setVoMapList);
    }
}
